package jsu.lipengcheng.trafficnet.service.Impl;

import jsu.lipengcheng.trafficnet.Util.JwtUtil;
import jsu.lipengcheng.trafficnet.common.Adminlogin;
import jsu.lipengcheng.trafficnet.handler.BenEX;
import jsu.lipengcheng.trafficnet.handler.UserEX;
import jsu.lipengcheng.trafficnet.mapper.ActivityMapper;
import jsu.lipengcheng.trafficnet.mapper.ActivityUserMapper;
import jsu.lipengcheng.trafficnet.mapper.AdminMapper;
import jsu.lipengcheng.trafficnet.pojo.ActivityUser;
import jsu.lipengcheng.trafficnet.pojo.Admins;
import jsu.lipengcheng.trafficnet.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityUserMapper activityUserMapper;


    /**
     * 管理员登录
     * @param admins
     * @return
     */
    public Adminlogin login(Admins admins) {
        String usernamel = admins.getUsername();
        Admins admins1= adminMapper.selectbyusername(usernamel);
        if(admins1==null){
            throw new UserEX("用户名或密码错误");
        }
        else if(!admins1.getPassword().equals(admins.getPassword())){
            throw new UserEX("用户名或密码错误");
        }
        else {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("userid",admins1.getId());
            String token = JwtUtil.createJWT("lipengchengajjahjkhadwid", 3600000L, map);
            Adminlogin adminlogin = Adminlogin.builder().userid(admins1.getId()).token(token).build();
            return adminlogin;
        }
    }

    @Transactional
    public void extracted(ActivityUser activityUser) {
        //活动数量减一
        activityMapper.updatenum(activityUser.getActivityId());
        //创建活动订单
        activityUserMapper.insert(activityUser);
    }
}
